package com.copa.l720;

import java.util.HashSet;
import java.util.Set;

/**
 * @author copa
 * @createDate 2022-07-03 20:51
 * @function set+暴力法
 */
public class Test1 {

    public String longestWord(String[] words) {
        Set<String> set = new HashSet<>();
        for (String word : words) {
            set.add(word);
        }
        String result = "";
        for (String word : words) {
            boolean flag = true;
            for (int i = 1; i < word.length(); i++) {
                if (!set.contains(word.substring(0, i))) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                if (result.length() == word.length()) {
                    result = result.compareTo(word) < 0 ? result : word;
                    continue;
                }
                result = result.length() > word.length() ? result : word;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        //String[] words = {"a", "banana", "app", "appl", "ap", "apply", "apple"};
        String[] words = {"yo","ew","fc","zrc","yodn","fcm","qm","qmo","fcmz","z","ewq","yod","ewqz","y"};
        System.out.println(new Test1().longestWord(words));
    }
}
